package com.cat.dataStructure03;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-seconds-to-equalize-a-circular-array/
 * @create 2025/11/9 09:35
 * @since JDK17
 */

public class Solution26 {
    public int minimumSeconds(List<Integer> nums) {
        int n = nums.size(), ans = n;
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            map.computeIfAbsent(nums.get(i), x -> new ArrayList<>()).add(i);
        }
        for (var p : map.values()) {
            int max = n - p.getLast() + p.getFirst();
            for (int i = 1; i < p.size(); i++) {
                max = Math.max(max, p.get(i) - p.get(i - 1));
            }
            ans = Math.min(ans, max);
        }

        return ans / 2;
    }
}
